##########################################################################################################
#Replication data for Gelman et al's The Consequences of Using a Top 5 System in Nevada

#Created March 10, 2004
#Most recent update: September 17, 2024
#########################################################################################################
rm(list=ls())
options(max.print=10000)

require(ggplot2)
require(dplyr)
require(stringr)
require(gridExtra)

setwd("C:/replication/") #Set your working directory

#Table 2
noms<- read.csv("generic_primary.csv") #Read in data
noms$name = word(noms$generic_primary,-1)
noms$name[noms$name == "Jr."] <- "Kennedy Jr."
nom_count <- noms %>% group_by(dem) %>% tally() #Count # nominees at each level
generic_summary <- noms%>%
  group_by(name)%>% 
  summarise(Mean=mean(win_perc), Max=max(win_perc), Min=min(win_perc), Median=median(win_perc), Std=sd(win_perc))  
generic_summary

gov22_noms<-read.csv("gov22_primary.csv")
gov22nom_count <- gov22_noms %>% group_by(dem) %>% tally() #Count # nominees at each level
gov22_summary <- gov22_noms%>%
  group_by(gov22_primary)%>% 
  summarise(Mean=mean(win_perc), Max=max(win_perc), Min=min(win_perc), Median=median(win_perc), Std=sd(win_perc))  
gov22_summary

sen24_noms<-read.csv("sen24_primary.csv")
sen24nom_count <- sen24_noms %>% group_by(dem) %>% tally() #Count # nominees at each level
sen24_summary <- sen24_noms%>%
  group_by(sen24_primary)%>% 
  summarise(Mean=mean(win_perc), Max=max(win_perc), Min=min(win_perc), Median=median(win_perc), Std=sd(win_perc))  
sen24_summary

gov26_noms<-read.csv("gov26_primary.csv")
gov26_noms$gov26_primary[gov26_noms$gov26_primary == "Barry Rubinson (Independent American)"] <- "Barry Rubinson (IAP)"
gov26nom_count <- gov26_noms %>% group_by(dem) %>% tally() #Count # nominees at each level
gov26_summary <- gov26_noms%>%
  group_by(gov26_primary)%>% 
  summarise(Mean=mean(win_perc), Max=max(win_perc), Min=min(win_perc), Median=median(win_perc), Std=sd(win_perc))  
gov26_summary

#Primary plots (Appendix)
gen_plot<-ggplot(noms[which(noms$win_perc<1), ], mapping=aes(x=dem, y=win_perc, group=name)) +
  geom_line() + geom_point() + 
  facet_wrap(~name) + theme(legend.position='none') + 
  ylab("Advance to General - Proportion") + 
  xlab("% of Democrats in Simulated Electorates") + ggtitle("Generic Primary")

gov22_plot<-ggplot(gov22_noms[which(gov22_noms$win_perc<1), ], mapping=aes(x=dem, y=win_perc, group=gov22_primary)) +
  geom_line() + geom_point() + 
  facet_wrap(~gov22_primary) + theme(legend.position='none') + 
  ylab("Advance to General - Proportion") + 
  xlab("% of Democrats in Simulated Electorates") + ggtitle("Governor 2022 Primary")

sen24_plot<-ggplot(sen24_noms[which(sen24_noms$win_perc<1), ], mapping=aes(x=dem, y=win_perc, group=sen24_primary)) +
  geom_line() + geom_point() + 
  facet_wrap(~sen24_primary) + theme(legend.position='none') + 
  ylab("Advance to General - Proportion") + 
  xlab("% of Democrats in Simulated Electorates") + ggtitle("Senate 2024 Primary")

gov26_plot<-ggplot(gov26_noms[which(gov26_noms$win_perc<1), ], mapping=aes(x=dem, y=win_perc, group=gov26_primary)) +
  geom_line() + geom_point() + 
  facet_wrap(~gov26_primary) + theme(legend.position='none') + 
  ylab("Advance to General - Proportion") + 
  xlab("% of Democrats in Simulated Electorates") + ggtitle("Governor 2026 Primary")

#General election plots (Figure 1)
generic_gen<- read.csv("generic_general.csv")
gengen_plot<-ggplot(generic_gen, aes(dem)) +   
  geom_line(aes(y = obama_perc), color = "black") + 
  geom_line(aes(y = trump_perc), color = "white") + 
  geom_label(label="Obama", x=44.8, y=0.95, #label.padding = unit(0.55, "lines"), 
             label.size = 0.35, color = "black") + 
  ylab("Proportion Candidate Wins General Election") + xlab("% of Democrats in Simulated Electorates") +
  ggtitle("Generic")

gov22_gen<- read.csv("gov22_general.csv")
gengov22_plot<-ggplot(gov22_gen, aes(dem)) +   
  geom_line(aes(y = sisolak_perc), color = "blue") + 
  geom_line(aes(y = lombardo_perc), color = "red") + 
  geom_label(label="Sisolak", x=44.8, y=0.95, #label.padding = unit(0.55, "lines"), 
    label.size = 0.35, color = "black") + 
    geom_label(label="Lombardo", x=44.8, y=0.05, #label.padding = unit(0.55, "lines"),
               label.size = 0.35,color = "black") + 
  ylab("Proportion Candidate Wins General Election") + xlab("% of Democrats in Simulated Electorates") +
  ggtitle("Governor 2022")

sen24_gen<- read.csv("sen24_general.csv")
gensen24_plot<-ggplot(sen24_gen, aes(dem)) +   
  geom_line(aes(y = rosen_perc), color = "blue") + 
  geom_line(aes(y = sandoval_perc), color = "red") + 
  geom_point(aes(x = 26, y =0.0003), color = "magenta") + 
  geom_label(label="Rosen", x=44.8, y=0.95, #label.padding = unit(0.55, "lines"), 
             label.size = 0.35, color = "black") + 
  geom_label(label="Sandoval", x=44.8, y=0.05, #label.padding = unit(0.55, "lines"),
             label.size = 0.35,color = "black") + 
  geom_label(label="Marchant", x=24.9, y=0, #label.padding = unit(0.55, "lines"),
             label.size = 0.24,color = "black") + 
  ylab("Proportion Candidate Wins General Election") + xlab("% of Democrats in Simulated Electorates") +
  ggtitle("Senate 2024")

gov26_gen<- read.csv("gov26_general.csv")
gengov26_plot<-ggplot(gov26_gen, aes(dem)) +   
  geom_line(aes(y = ford_perc), color = "blue") + 
  geom_line(aes(y = lombardo_perc), color = "red") + 
  geom_label(label="Ford", x=44.8, y=0.95, #label.padding = unit(0.55, "lines"), 
             label.size = 0.35, color = "black") + 
  geom_label(label="Lombardo", x=44.8, y=0.05, #label.padding = unit(0.55, "lines"),
             label.size = 0.35,color = "black") + 
  ylab("Proportion Candidate Wins General Election") + xlab("% of Democrats in Simulated Electorates") +
  ggtitle("Governor 2026")

####Data from survey####
#Descriptive Statistics Crossover voting analysis
setwd("~replication")

#Read in the dynata and convenience samples
dynata<- read.csv("dynata_numeric.csv") #Dynata
con<- read.csv("con_numeric.csv") #Convenience sample
data<-dplyr::bind_rows(dynata, con) #Bind samples together
data$omit[is.na(data$omit)] <- 0 #Replace NA's in omit columns with 0
data2<-subset(data, data$omit==0) #Remove click-throughs and incomplete responses (keep omit=0)

#Descriptive statistics
data2%>% count(metro)
data2%>% count(age)
data2%>% count(hispanic)
data2%>% count(race)
data2%>% count(education)
data2%>% count(gender)
data2%>% count(Q1)
data2%>% group_by(Q1) %>% 
          summarise(trumpmean = mean(feelthermometer_2, na.rm=TRUE), trump_median = median(feelthermometer_2, na.rm=TRUE), mcconnell_median = median(feelthermometer_6, na.rm=TRUE), cheney_median = median(feelthermometer_4, na.rm=TRUE), obama_median = median(feelthermometer_1, na.rm=TRUE))

data2%>%count(political_knowledge)

#For analyses that remove everyone who overvoted
#data4<-subset(data2, data2$gen_over!=1) #Remove everyone who only overvoted in generic
#data5<-subset(data2, data2$gov22_over!=1) #Remove everyone who only overvoted in gov22
#data6<-subset(data2, data2$sen24_over!=1) #Remove everyone who only overvoted in sen24
#data7<-subset(data2, data2$gov26_over!=1) #Remove everyone who only overvoted in gov26

#First place votes
prop.table(table(data2$generic_general_1)) #Obama
prop.table(table(data2$generic_general_2)) #Trump
prop.table(table(data2$generic_general_3)) #Sanders
prop.table(table(data2$generic_general_5)) #Romney
#Raw first votes as denominator for crossover voting
table(data2$generic_general_1) #Obama
table(data2$generic_general_2) #Trump
addmargins(table(data2$generic_general_3)) #Sanders
addmargins(table(data2$generic_general_5)) #Romney

#Table 6
#Trump or Romney AFTER both voted for Obama and Sanders
data3 <- subset(data2, (generic_general_1==6 & generic_general_3==7) | (generic_general_1==7 & generic_general_3==6)) 
prop.table(table(data3$generic_general_2)) #Trump
prop.table(table(data3$generic_general_5)) #Romney

#Obama or Sanders AFTER both voted for Trump and Romney
data3 <- subset(data2, (generic_general_2==6 & generic_general_5==7) | (generic_general_2==7 & generic_general_5==6)) 
prop.table(table(data3$generic_general_1)) #Obama
prop.table(table(data3$generic_general_3)) #Sanders

#Table 5
#For table 5, the denominator are the raw first place votes received. 
table(data2$generic_general_1) #Obama
table(data2$generic_general_2) #Trump
table(data2$generic_general_3) #Sanders
table(data2$generic_general_5) #Romney

#Obama/Romney to Sanders/Trump (Same party moderate to extremist)
addmargins(table(data2$generic_general_1, data2$generic_general_3))
addmargins(table(data2$generic_general_5, data2$generic_general_2))
(16+125)/(193+37)
(13+107)/(161+32) #No overvotes

#Sanders/Trump to Obama/Romney (Same party extremist to moderate)
addmargins(table(data2$generic_general_1, data2$generic_general_3))
addmargins(table(data2$generic_general_5, data2$generic_general_2))
(52+41)/(138+62)

#Obama & Romney (Other party moderate)
addmargins(table(data2$generic_general_1, data2$generic_general_5))
(5+23)/(193+37)
(22+5)/(161+32) #No overvotes

#Obama/Romney to Trump/Sanders (Moderate to other party extremist)
addmargins(table(data2$generic_general_1, data2$generic_general_2))
addmargins(table(data2$generic_general_3, data2$generic_general_5))
(15+8)/(193+37)
(13+7)/(161+32) #No overvotes

#Trump/Sanders to Romney/Obama (Extremist to same party moderate)
addmargins(table(data2$generic_general_1, data2$generic_general_2))
addmargins(table(data2$generic_general_5, data2$generic_general_3))
(19+8)/(138+62)
(17+7)/(120+54) #No overvotes

#Trump to/from Sanders (Extremist to other party extremist)
addmargins(table(data2$generic_general_2, data2$generic_general_3))
(17+2)/(138+62)
(15+2)/(120+54) #No overvote

#Trump/Sanders to Romney/Obama (Extremist to other party extremist)
addmargins(table(data2$generic_general_2, data2$generic_general_5))
addmargins(table(data2$generic_general_2, data2$generic_general_5))

#First place votes
prop.table(table(data2$Q50_1)) #Gilbert
prop.table(table(data2$Q50_3)) #Heller
prop.table(table(data2$Q50_4)) #Lombardo
prop.table(table(data2$Q50_5)) #Sisolak
#Raw first votes as denominator for crossover voting
table(data2$Q50_1) #Gilbert
table(data2$Q50_3) #Heller
table(data2$Q50_4) #Lombardo
table(data2$Q50_5) #Sisolak

#Gilbert - Heller
addmargins(table(data2$Q50_1, data2$Q50_3)) 
#Gilbert - Lombardo
addmargins(table(data2$Q50_1, data2$Q50_4)) 
#Gilbert - Sisolak
addmargins(table(data2$Q50_1, data2$Q50_5)) 
#Heller - Lombardo
addmargins(table(data2$Q50_3, data2$Q50_4)) 
#Sisolak - Heller
addmargins(table(data2$Q50_5, data2$Q50_3)) 
#Sisolak - Lombardo
addmargins(table(data2$Q50_5, data2$Q50_4)) 

#Moderate to same party (Heller/Lombardo)
(69+19)/(38+128)
(52+14)/(25+89) #No overvotes

#Moderate to other party moderate (Heller/Lombardo and Sisolak)
(68+16+4+18)/(200+38+128)
(12+57+3+10)/(135+25+89) #No overvotes

#Moderate to other party extremist (Sisolak to Gilbert)
74/200

#Moderate to Extremist to same party (Heller and Lombardo to Gilbert) 
(6+5)/(38+128)

#Extremist to same party moderate (Gilbert to Heller/Sisolak)
(6+5)/48

#Gilbert to Sisolak (Extremist to other party moderate)
16/48

##Senate 24
#First place votes
prop.table(table(data2$Q52_2)) #Marchant
prop.table(table(data2$Q52_3)) #Rosen
prop.table(table(data2$Q52_4)) #Sandvoal
prop.table(table(data2$Q52_5)) #Segerblom
#Raw first votes as denominator for crossover voting
table(data2$Q52_2) #Marchant
table(data2$Q52_3) #Rosen
table(data2$Q52_4) #Sandoval
table(data2$Q52_5) #Segerblom

#Rosen or Segerblom AFTER both voted for Marchant and Sandoval
data3 <- subset(data2, (Q52_2==1 & Q52_4==2) | (Q52_2==2 & Q52_4==1)) 
prop.table(table(data3$Q52_5)) #Segerblom
prop.table(table(data3$Q52_3)) #Rosen

#Marchant or Sandoval AFTER both voted for Rosen and Segerblom
data3 <- subset(data2, (Q52_5==1 & Q52_3==2) | (Q52_5==2 & Q52_3==1)) 
prop.table(table(data3$Q52_2)) #Marchant
prop.table(table(data3$Q52_4)) #Sandoval

#Marchant-Sandoval
addmargins(table(data5$Q52_2, data5$Q52_4)) 
#Rosen-Segerblom
addmargins(table(data5$Q52_3, data5$Q52_5)) 
#Rosen-Marchant
addmargins(table(data5$Q52_3, data5$Q52_2)) 
#Sandoval-Segerblom
addmargins(table(data5$Q52_4, data5$Q52_5)) 
#Rosen_Sandoval 
addmargins(table(data5$Q52_3, data5$Q52_4))
#Marchant-Segerblom
addmargins(table(data5$Q52_2, data5$Q52_5)) 

#Moderate to moderate (Rosen and Sandoval)
(17+35)/(107+161)
(12+29)/(124+86) #No overvotes
#Moderates to same party extremist (Rosen/Segerlom & Sandoval/Marchant)
(92+58)/(107+161)
#Other party extremist (Rosen to Marchant/Sandoval to Segerblom)
(7+9)/(107+161)

#Extremist to extremist (Segerblom and Marchant)
(5+2)/(50+59)
#Extremist to same party
(31+23)/(50+59)
#Other party moderate
(3+11)/(50+59)

##Governor 26
#First place votes
prop.table(table(data2$gov26_general_1)) #Ford
prop.table(table(data2$gov26_general_2)) #Gilbert
prop.table(table(data2$gov26_general_4)) #Kirkpatrick
prop.table(table(data2$gov26_general_5)) #Lombardo
#Raw first votes as denominator for crossover voting
table(data2$gov26_general_1) #Ford
table(data2$gov26_general_2) #Gilbert
table(data2$gov26_general_4) #Kirkpatrick
table(data2$gov26_general_5) #Lombardo

#Lombardo or Gilbert AFTER both voted for Kirkpatrick and Ford
data3 <- subset(data2, (gov26_general_1==1 & gov26_general_4==2) | (gov26_general_1==2 & gov26_general_4==1)) 
prop.table(table(data3$gov26_general_2)) #Gilbert
prop.table(table(data3$gov26_general_5)) #Lombardo

#Ford/Lomabrdo
addmargins(table(data2$gov26_general_1, data2$gov26_general_5)) 
#Ford/Kikrpatrick
addmargins(table(data2$gov26_general_1, data2$gov26_general_4)) 
#Lombardo/Kikrpatrick
addmargins(table(data2$gov26_general_5, data2$gov26_general_4)) 
#Lombardo/Glibert
addmargins(table(data2$gov26_general_5, data2$gov26_general_2)) 
#Ford/Gilbert
addmargins(table(data2$gov26_general_1, data2$gov26_general_2)) 
#Kirkpatrick/Gilbert
addmargins(table(data2$gov26_general_4, data2$gov26_general_2)) 

#Ford and Kirkpatrick
(84+35)/(132+58)
#Moderate to moderate (Ford/Kirkpatrick to and from Lombardo)
(7+20+11+6)/(132+58+146)
#Lombardo to Gilbert
78/146
#Ford/Kirkpatrick to Gilbert
(5+18)/(132+58)
#Gilbert to Lombardo
17/30
#Gilbert to Ford/Kirkpatrick
(3+1)/30